package com.wangxl.springbootdemocore.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName:Dates
 * @Description:
 * @Author : Wangxl
 * @Email：475524154@qq.com
 * @Date:2018/12/7 000716:26
 * @Version:1.0
 **/

public class Dates {
    /**
     * 将日期变成字符串
     *
     * @param date
     * @return
     */
    public String dateStr(Date date)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(ConstatFinalUtil.DATE_FORMAT);
        return sdf.format(date);
    }

    /**
     * 将日期时间变成字符串
     *
     * @param date
     * @return
     */
    public String dateTimeStr(Date date)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(ConstatFinalUtil.DATETIME_FORMAT);
        return sdf.format(date);
    }

    /**
     * 将日期变成字符串
     *
     * @param date
     * @param pattern 日期时间的格式
     * @return
     */
    public String dateStr(Date date,String pattern)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 将日期变成字符串
     *
     * @param date
     * @return
     */
    public Date strDate(String now)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(ConstatFinalUtil.DATE_FORMAT);
        try
        {
            return sdf.parse(now);
        } catch (ParseException e)
        {
        }
        return null ;
    }

    /**
     * 将日期时间变成字符串
     *
     * @param date
     * @return
     */
    public Date strDateTime(String now)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(ConstatFinalUtil.DATETIME_FORMAT);
        try
        {
            return sdf.parse(now);
        } catch (ParseException e)
        {
        }
        return null ;
    }

    /**
     * 将日期变成字符串
     *
     * @param date
     * @param pattern 日期时间的格式
     * @return
     */
    public Date strDate(String now,String pattern)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try
        {
            return sdf.parse(now);
        } catch (ParseException e)
        {
        }
        return null ;
    }
}
